"""
Case Type   : cm
Case Name   : cm_ctl reload在线加载cm实例静态配置文件基本参数验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、reload在线加载实例静态配置文件
    3、加载可以动态生效的agent参数
    4、加载可以动态生效的server参数
    5、指定多个参数
    6、指定错误参数
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、加载成功
    3、加载成功
    4、加载成功
    5、加载成功，以后一个为准
    6、合理报错
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:reload在线加载实例静态配置文件; expect:加载成功----'
        self.log.info(text)

        load_res1 = self.comsh.exec_cm_ctl('reload')
        self.log.info(load_res1)
        self.assertIn('cm_ctl reload success', load_res1, '加载失败')

        text = '----step3:加载可以动态生效的agent参数  expect:加载成功----'
        self.log.info(text)

        load_res2 = self.comsh.exec_cm_ctl('reload', '--param --agent')
        self.log.info(load_res2)
        self.assertIn(self.constant.cma_reload_success_msg, load_res2, '加载失败')

        text = '----step4:加载可以动态生效的server参数  expect:加载成功----'
        self.log.info(text)

        load_res3 = self.comsh.exec_cm_ctl('reload', '--param --server')
        self.log.info(load_res3)
        self.assertIn(self.constant.cms_reload_success_msg, load_res3, '加载失败')

        text = '----step5:指定多个参数  expect:加载成功，以后一个参数为准----'
        self.log.info(text)

        load_res4 = self.comsh.exec_cm_ctl('reload',
                                           '--param --server --agent')
        self.log.info(load_res4)
        self.assertIn(self.constant.cma_reload_success_msg, load_res4, '加载失败')

        text = '----step6:指定错误参数  expect:合理报错----'
        self.log.info(text)

        param_list = ['--param', '--server', '--agent']
        for param in param_list:
            load_res5 = self.comsh.exec_cm_ctl('reload', f'{param}')
            self.assertIn('command wrong', load_res5, '参数查看成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
